package com.woniu.bank.controller;

import cn.hutool.core.date.CalendarUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.bank.dto.*;
import com.woniu.bank.entity.Rated;
import com.woniu.bank.service.ClientInfoSheetService;
import com.woniu.bank.service.RatedService;
import com.woniu.bank.util.Result;
import com.woniu.bank.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author ：zhulongxing
 * @date ：Created in 2023/1/4 15:41
 * @description：
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping("ratedMains")
@Api(tags = "评级授信主界面接口",description = "评级授信主界面表格所有接口")
public class RatedMainController {

    Calendar lastDate  = Calendar.getInstance();

    @Autowired
    private RatedService ratedService;

    @Autowired
    private ClientInfoSheetService clientInfoSheetService;

    @ApiOperation(value = "评级授信主界面表格信息接口",notes ="展示所有客户信息",httpMethod="POST")
    @PostMapping("selectMainAll")
    public Result selectMainAll(@RequestBody RatedMainDTO ratedMainDTO){
        List<RatedMainVO> lists = ratedService.selectOne(ratedMainDTO);
        PageInfo<RatedMainVO> ratedMainVOS = new PageInfo<>(lists);
        return Result.ok().data("ratedMainVOs",ratedMainVOS);
    }

    @ApiOperation(value = "评级授信引入客户展示表格信息接口",notes ="展示所有客户信息",httpMethod="POST")
    @PostMapping("selectAddAll")
    public Result selectAddAll(@RequestBody RatedDTO ratedAddDTO){
        List<RatedVO> lists = clientInfoSheetService.selectAdd(ratedAddDTO);
        PageInfo<RatedVO> ratedAddVOS = new PageInfo<>(lists);
        return Result.ok().data("ratedMainVOs",ratedAddVOS);
    }

    @ApiOperation(value = "评级修改客户信息展示接口",notes ="展示客户信息",httpMethod="GET")
    @GetMapping("{clientId}")
    public Result selectByKey(@PathVariable("clientId") Long clientId){
        Rated rated = ratedService.selectByKey(clientId);
        return Result.ok().data("rated",rated);
    }

    @ApiOperation(value = "获得机评得分接口",notes ="客户评级",httpMethod="POST")
    @PostMapping("jfcp")
    public Result jfcp(@RequestBody RatedAddDTO ratedAddDTO) {
        RatedAddVO ratedAddVO = new RatedAddVO();
        if(Double.valueOf(ratedAddDTO.getClinetDocumentsNumber())>=0&&ratedAddDTO.getClientIncome()>=0&&ratedAddDTO.getClientMainIncome()>=0&&ratedAddDTO.getClientIndustry()>=0&&ratedAddDTO.getFarmerYearRevenue()>=0&&ratedAddDTO.getFarmerYearExpenditures()>=0) {
            int nj, zj, rj, gs;
            if (ratedAddDTO.getClientIncome() >= 300000) {
                nj = 30;
            } else if (ratedAddDTO.getClientIncome() >= 200000 && ratedAddDTO.getClientIncome() < 300000) {
                nj = 28;
            } else if (ratedAddDTO.getClientIncome() >= 100000 && ratedAddDTO.getClientIncome() < 200000) {
                nj = 26;
            } else if (ratedAddDTO.getClientIncome() >= 50000 && ratedAddDTO.getClientIncome() < 10000) {
                nj = 22;
            } else if (ratedAddDTO.getClientIncome() >= 30000 && ratedAddDTO.getClientIncome() < 50000) {
                nj = 20;
            } else if (ratedAddDTO.getClientIncome() > 10000 && ratedAddDTO.getClientIncome() < 30000) {
                nj = 18;
            } else {
                nj = 15;
            }
            if (ratedAddDTO.getClientMainIncome() >= 500000) {
                zj = 30;
            } else if (ratedAddDTO.getClientMainIncome() >= 400000 && ratedAddDTO.getClientMainIncome() < 500000) {
                zj = 28;
            } else if (ratedAddDTO.getClientMainIncome() >= 300000 && ratedAddDTO.getClientMainIncome() < 400000) {
                zj = 26;
            } else if (ratedAddDTO.getClientMainIncome() >= 200000 && ratedAddDTO.getClientMainIncome() < 300000) {
                zj = 22;
            } else if (ratedAddDTO.getClientMainIncome() >= 100000 && ratedAddDTO.getClientMainIncome() < 100000) {
                zj = 20;
            } else if (ratedAddDTO.getClientMainIncome() > 50000 && ratedAddDTO.getClientMainIncome() < 100000) {
                zj = 18;
            } else {
                zj = 15;
            }
            if ((ratedAddDTO.getClientMainIncome() / 365) >= 100000) {
                rj = 15;
            } else if ((ratedAddDTO.getClientMainIncome() / 365) >= 50000 && (ratedAddDTO.getClientMainIncome() / 365) < 100000) {
                rj = 14;
            } else if ((ratedAddDTO.getClientMainIncome() / 365) >= 10000 && (ratedAddDTO.getClientMainIncome() / 365) < 50000) {
                rj = 13;
            } else if ((ratedAddDTO.getClientMainIncome() / 365) >= 5000 && (ratedAddDTO.getClientMainIncome() / 365) < 10000) {
                rj = 9;
            } else if ((ratedAddDTO.getClientMainIncome() / 365) >= 1000 && (ratedAddDTO.getClientMainIncome() / 365) < 5000) {
                rj = 8;
            } else if ((ratedAddDTO.getClientMainIncome() / 365) > 0 && (ratedAddDTO.getClientMainIncome() / 365) < 1000) {
                rj = 7;
            } else {
                rj = 0;
            }
            if (ratedAddDTO.getClientIndustry() > 5) {
                gs = 15;
            } else if (ratedAddDTO.getClientIndustry() == 5) {
                gs = 14;
            } else if (ratedAddDTO.getClientIndustry() == 4) {
                gs = 13;
            } else if (ratedAddDTO.getClientIndustry() == 3) {
                gs = 9;
            } else if (ratedAddDTO.getClientIndustry() == 2) {
                gs = 8;
            } else if (ratedAddDTO.getClientIndustry() == 1) {
                gs = 7;
            } else {
                gs = 0;
            }
            ratedAddVO.setRatedRobotScore(String.valueOf(nj + zj + rj + gs));
            if ((nj + zj + rj + gs) >= 90) {
                ratedAddVO.setRatedCredit("特优");
            } else if ((nj + zj + rj + gs) >= 80 && (nj + zj + rj + gs) < 90) {
                ratedAddVO.setRatedCredit("优秀");
            } else if ((nj + zj + rj + gs) >= 70 && (nj + zj + rj + gs) < 80) {
                ratedAddVO.setRatedCredit("较好");
            } else if ((nj + zj + rj + gs) >= 50 && (nj + zj + rj + gs) < 70) {
                ratedAddVO.setRatedCredit("一般");
            } else if ((nj + zj + rj + gs) >= 30 && (nj + zj + rj + gs) < 50) {
                ratedAddVO.setRatedCredit("普惠级");
            } else {
                ratedAddVO.setRatedCredit("置灰");
            }
            Date date = new Date();
            ratedAddVO.setRatedOn(date.getTime() + "");
            if (ratedAddDTO.getFarmerYearRevenue() >= ratedAddDTO.getFarmerYearExpenditures()) {
                if (ratedAddVO.getRatedCredit().equals("特优")) {
                    BigDecimal decimal = new BigDecimal((ratedAddDTO.getFarmerYearRevenue() - ratedAddDTO.getFarmerYearExpenditures()) * 2 * 1);
                    ratedAddVO.setRatedRobotLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                    ratedAddVO.setRatedCreditLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                } else if (ratedAddVO.getRatedCredit().equals("优秀")) {
                    BigDecimal decimal = new BigDecimal((ratedAddDTO.getFarmerYearRevenue() - ratedAddDTO.getFarmerYearExpenditures()) * 2 * 0.9);
                    ratedAddVO.setRatedRobotLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                    ratedAddVO.setRatedCreditLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                } else if (ratedAddVO.getRatedCredit().equals("较好")) {
                    BigDecimal decimal = new BigDecimal((ratedAddDTO.getFarmerYearRevenue() - ratedAddDTO.getFarmerYearExpenditures()) * 2 * 0.8);
                    ratedAddVO.setRatedRobotLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                    ratedAddVO.setRatedCreditLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                } else if (ratedAddVO.getRatedCredit().equals("一般")) {
                    BigDecimal decimal = new BigDecimal((ratedAddDTO.getFarmerYearRevenue() - ratedAddDTO.getFarmerYearExpenditures()) * 2 * 0.7);
                    ratedAddVO.setRatedRobotLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                    ratedAddVO.setRatedCreditLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                } else if (ratedAddVO.getRatedCredit().equals("普惠级")) {
                    BigDecimal decimal = new BigDecimal((ratedAddDTO.getFarmerYearRevenue() - ratedAddDTO.getFarmerYearExpenditures()) * 2 * 0.6);
                    ratedAddVO.setRatedRobotLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                    ratedAddVO.setRatedCreditLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                }
            }
            Date date1 = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("MMdd");

            ratedAddVO.setRatedNum(sdf.format(date1) + ratedAddDTO.getClinetDocumentsNumber());
        }
        return Result.ok().data("ratedAddVO",ratedAddVO);
    }
    @ApiOperation(value = "评级新增提交接口",notes ="客户评级新增",httpMethod="POST")
    @PostMapping("add")
    public Result add(@RequestBody Rated rated){
        if(Integer.valueOf(rated.getRatedRobotScore())<=100&&Integer.valueOf(rated.getRatedRobotScore())>=30) {
            lastDate.setTime(rated.getRatedRequestTime());
            lastDate.add(Calendar.MONTH, Integer.valueOf(rated.getRatedTerm()));// 加一个年
            rated.setRatedEndTime(new Date(lastDate.getTime().getTime()));
            rated.setRatedStartTime(rated.getRatedRequestTime());
            rated.setRatedExpireTime(new Date(lastDate.getTime().getTime()));
            ratedService.insertSelective(rated);
        }
        return Result.ok();
    }
    @ApiOperation(value = "评级修改提交接口",notes ="客户评级修改",httpMethod="PUT")
    @PutMapping("update")
    public Result update(@RequestBody Rated rated){

        lastDate.setTime(rated.getRatedRequestTime());
        lastDate.add(Calendar.MONTH, Integer.valueOf(rated.getRatedTerm()));
        rated.setRatedEndTime(new Date(lastDate.getTime().getTime()));
        rated.setRatedStartTime(rated.getRatedRequestTime());
        rated.setRatedExpireTime(new Date(lastDate.getTime().getTime()));
        ratedService.updateByPrimaryKeySelective(rated);
        return Result.ok();
    }

    @ApiOperation(value = "评级授信额度调整主界面表格信息接口",notes ="展示额度调整所有客户信息",httpMethod="POST")
    @PostMapping("selectUpdateAll")
    public Result selectUpdateAll(@RequestBody RatedUpdateDTO ratedUpdateDTO){
        List<RatedUpdateVO> lists = ratedService.selectTwo(ratedUpdateDTO);
        PageInfo<RatedUpdateVO> ratedUpdateVOs = new PageInfo<>(lists);
        return Result.ok().data("ratedUpdateVOs",ratedUpdateVOs);
    }

    @ApiOperation(value = "评级授信额度调整业务信息界面提交接口",notes ="额度调整修改客户信息",httpMethod="PUT")
    @PutMapping("clientAndRated")
    public Result clientAndRated(@RequestBody ClientAndRatedDTO clientAndRatedDTO){
        lastDate.setTime(clientAndRatedDTO.getRatedRequestTime());
        lastDate.add(Calendar.MONTH, Integer.valueOf(clientAndRatedDTO.getRatedTerm()));
        clientAndRatedDTO.setRatedEndTime(new Date(lastDate.getTime().getTime()));
        clientAndRatedDTO.setRatedStartTime(clientAndRatedDTO.getRatedRequestTime());
        clientAndRatedDTO.setRatedExpireTime(new Date(lastDate.getTime().getTime()));
        int i = ratedService.updateClientAndRated(clientAndRatedDTO);
        return Result.ok();
    }

    @ApiOperation(value = "额度调整机评得分接口",notes ="客户重新评级",httpMethod="POST")
    @PostMapping("cxpj")
    public Result cxpj(@RequestBody RatedAddDTO ratedAddDTO) {
        //授信协议号和流水号不需要，所以是空的
        RatedAddVO ratedAddVO = new RatedAddVO();
        if(Double.valueOf(ratedAddDTO.getClinetDocumentsNumber())>=0&&ratedAddDTO.getClientIncome()>=0&&ratedAddDTO.getClientMainIncome()>=0&&ratedAddDTO.getClientIndustry()>=0&&ratedAddDTO.getFarmerYearRevenue()>=0&&ratedAddDTO.getFarmerYearExpenditures()>=0) {
            int nj, zj, rj, gs;
            if (ratedAddDTO.getClientIncome() >= 300000) {
                nj = 30;
            } else if (ratedAddDTO.getClientIncome() >= 200000 && ratedAddDTO.getClientIncome() < 300000) {
                nj = 28;
            } else if (ratedAddDTO.getClientIncome() >= 100000 && ratedAddDTO.getClientIncome() < 200000) {
                nj = 26;
            } else if (ratedAddDTO.getClientIncome() >= 50000 && ratedAddDTO.getClientIncome() < 10000) {
                nj = 22;
            } else if (ratedAddDTO.getClientIncome() >= 30000 && ratedAddDTO.getClientIncome() < 50000) {
                nj = 20;
            } else if (ratedAddDTO.getClientIncome() > 10000 && ratedAddDTO.getClientIncome() < 30000) {
                nj = 18;
            } else {
                nj = 15;
            }
            if (ratedAddDTO.getClientMainIncome() >= 500000) {
                zj = 30;
            } else if (ratedAddDTO.getClientMainIncome() >= 400000 && ratedAddDTO.getClientMainIncome() < 500000) {
                zj = 28;
            } else if (ratedAddDTO.getClientMainIncome() >= 300000 && ratedAddDTO.getClientMainIncome() < 400000) {
                zj = 26;
            } else if (ratedAddDTO.getClientMainIncome() >= 200000 && ratedAddDTO.getClientMainIncome() < 300000) {
                zj = 22;
            } else if (ratedAddDTO.getClientMainIncome() >= 100000 && ratedAddDTO.getClientMainIncome() < 100000) {
                zj = 20;
            } else if (ratedAddDTO.getClientMainIncome() > 50000 && ratedAddDTO.getClientMainIncome() < 100000) {
                zj = 18;
            } else {
                zj = 15;
            }
            if ((ratedAddDTO.getClientMainIncome() / 365) >= 100000) {
                rj = 15;
            } else if ((ratedAddDTO.getClientMainIncome() / 365) >= 50000 && (ratedAddDTO.getClientMainIncome() / 365) < 100000) {
                rj = 14;
            } else if ((ratedAddDTO.getClientMainIncome() / 365) >= 10000 && (ratedAddDTO.getClientMainIncome() / 365) < 50000) {
                rj = 13;
            } else if ((ratedAddDTO.getClientMainIncome() / 365) >= 5000 && (ratedAddDTO.getClientMainIncome() / 365) < 10000) {
                rj = 9;
            } else if ((ratedAddDTO.getClientMainIncome() / 365) >= 1000 && (ratedAddDTO.getClientMainIncome() / 365) < 5000) {
                rj = 8;
            } else if ((ratedAddDTO.getClientMainIncome() / 365) > 0 && (ratedAddDTO.getClientMainIncome() / 365) < 1000) {
                rj = 7;
            } else {
                rj = 0;
            }
            if (ratedAddDTO.getClientIndustry() > 5) {
                gs = 15;
            } else if (ratedAddDTO.getClientIndustry() == 5) {
                gs = 14;
            } else if (ratedAddDTO.getClientIndustry() == 4) {
                gs = 13;
            } else if (ratedAddDTO.getClientIndustry() == 3) {
                gs = 9;
            } else if (ratedAddDTO.getClientIndustry() == 2) {
                gs = 8;
            } else if (ratedAddDTO.getClientIndustry() == 1) {
                gs = 7;
            } else {
                gs = 0;
            }
            ratedAddVO.setRatedRobotScore(String.valueOf(nj + zj + rj + gs));
            System.out.println(ratedAddVO.getRatedRobotScore()+"===========================================");
            if ((nj + zj + rj + gs) >= 90) {
                ratedAddVO.setRatedCredit("特优");
            } else if ((nj + zj + rj + gs) >= 80 && (nj + zj + rj + gs) < 90) {
                ratedAddVO.setRatedCredit("优秀");
            } else if ((nj + zj + rj + gs) >= 70 && (nj + zj + rj + gs) < 80) {
                ratedAddVO.setRatedCredit("较好");
            } else if ((nj + zj + rj + gs) >= 50 && (nj + zj + rj + gs) < 70) {
                ratedAddVO.setRatedCredit("一般");
            } else if ((nj + zj + rj + gs) >= 30 && (nj + zj + rj + gs) < 50) {
                ratedAddVO.setRatedCredit("普惠级");
            } else {
                ratedAddVO.setRatedCredit("置灰");
            }
            if (ratedAddVO.getRatedCredit().equals("特优")) {
                BigDecimal decimal = new BigDecimal((ratedAddDTO.getFarmerYearRevenue() - ratedAddDTO.getFarmerYearExpenditures()) * 2 * 1);
                ratedAddVO.setRatedRobotLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                ratedAddVO.setRatedCreditLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else if (ratedAddVO.getRatedCredit().equals("优秀")) {
                BigDecimal decimal = new BigDecimal((ratedAddDTO.getFarmerYearRevenue() - ratedAddDTO.getFarmerYearExpenditures()) * 2 * 0.9);
                ratedAddVO.setRatedRobotLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                ratedAddVO.setRatedCreditLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else if (ratedAddVO.getRatedCredit().equals("较好")) {
                BigDecimal decimal = new BigDecimal((ratedAddDTO.getFarmerYearRevenue() - ratedAddDTO.getFarmerYearExpenditures()) * 2 * 0.8);
                ratedAddVO.setRatedRobotLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                ratedAddVO.setRatedCreditLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else if (ratedAddVO.getRatedCredit().equals("一般")) {
                BigDecimal decimal = new BigDecimal((ratedAddDTO.getFarmerYearRevenue() - ratedAddDTO.getFarmerYearExpenditures()) * 2 * 0.7);
                ratedAddVO.setRatedRobotLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                ratedAddVO.setRatedCreditLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            } else if (ratedAddVO.getRatedCredit().equals("普惠级")) {
                BigDecimal decimal = new BigDecimal((ratedAddDTO.getFarmerYearRevenue() - ratedAddDTO.getFarmerYearExpenditures()) * 2 * 0.6);
                ratedAddVO.setRatedRobotLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                ratedAddVO.setRatedCreditLimit(decimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            }
        }
        return Result.ok().data("ratedAddVO",ratedAddVO);
    }

    @ApiOperation(value = "额度调整业务信息根据id查询提交接口",notes ="查询某客户信息",httpMethod="POST")
    @PostMapping("{ratedId}")
    public Result selectById(@PathVariable("ratedId") Long ratedId){
        List<ClientAndRatedVO> clientAndRatedVOS = ratedService.selectByid(ratedId);
        return Result.ok().data("clientAndRatedVOS",clientAndRatedVOS);
    }

    @ApiOperation(value = "额度冻结展示列表信息接口",notes ="展示额度冻结页的授信列表信息",httpMethod="POST")
    @PostMapping("selectFrozen")
    public Result selectFrozen(@RequestBody RateFrozenDTO frozenDTO){
        List<RateFrozenVO> rateFrozenVOS = ratedService.selectByFrozen(frozenDTO);
        PageInfo<RateFrozenVO> frozenVOPageInfo=new PageInfo<>(rateFrozenVOS);
        return Result.ok().data("frozenVO",frozenVOPageInfo);
    }

    @ApiOperation(value = "额度冻结状态详情接口",notes ="展示冻结或解冻的授信详情",httpMethod="POST")
    @PostMapping("selectByFrozenMes/{id}")
    public Result selectByFrozenMes(@PathVariable("id") Long id){
        Rated rated = ratedService.selectByPrimaryKey(id);
        return Result.ok().data("FrozenMesRated",rated);
    }

    @ApiOperation(value = "额度冻结解冻接口",notes ="对授信额度进行冻结或解冻",httpMethod="POST")
    @PostMapping("updateFrozen")
    public Result updateFrozen(@RequestBody RateFrozenVO rated ){
        int i = ratedService.updateRateFrozen(rated);
        if(i>0){
            return Result.ok();
        }else{
            return Result.error("操作失败请重试");
        }

    }

}
